package persistencia;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import modelo.Aluno;


public class AlunoDAO extends Dao {
	public void inserir(Aluno aluno)throws Exception{
		open();
		
		stmt = con.prepareStatement("INSERT INTO aluno(matricula,nome) VALUES(?,?)");  
        
	      stmt.setString(2, aluno.getNome());  
	      stmt.setInt(1, aluno.getMatricula());  
	      	        
	      stmt.execute();        
	      close();        


	
	}
	
	public void deletar(Integer matricula)throws Exception{
		open();
		
		stmt = con.prepareStatement("DELETE FROM aluno WHERE MATRICULA =?");
		stmt.setInt(1, matricula);
		
		stmt.execute();        
	    close();		
	}
	
	public void atualizar(Aluno aluno)throws Exception{
		open();
		stmt = con.prepareStatement("UPDATE aluno SET NOME=? WHERE MATRICULA =?");
		stmt.setString(1, aluno.getNome());  
	    stmt.setInt(2, aluno.getMatricula());
	    
	    stmt.execute();
	    close();
	}
	
	public List<Aluno> getAllAlunos() throws Exception{
		List<Aluno> alunos = new ArrayList<Aluno>();
		open();
		ResultSet rSet = stmt.executeQuery("Select * from aluno");
		while(rSet.next()){
			Aluno aluno = new Aluno();
			aluno.setMatricula(rSet.getInt("matricula"));
			aluno.setNome(rSet.getString("nome"));
			
			alunos.add(aluno);
		}
		
		return alunos;

	}
	
	public Aluno getAlunoMaticula(Integer matricula) throws Exception{		
		open();
		Aluno aluno = new Aluno();
		ResultSet rSet = stmt.executeQuery("Select * from aluno where matricula = ?");
		stmt.setInt(1, matricula);
		if(rSet.next()){
			aluno.setMatricula(rSet.getInt("matricula"));
			aluno.setNome(rSet.getString("nome"));
		}
		
		return aluno;

	}

}
